package com.koron.order.risk.levelChange.service.impl;


import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.common.entity.TOrderNoMax;
import com.koron.common.core.util.CodeTools;
import com.koron.order.risk.base.bean.vo.LineRiskVO;
import com.koron.order.risk.base.service.LineRiskService;
import com.koron.order.risk.levelChange.bean.po.LineRiskLevel;
import com.koron.order.risk.levelChange.bean.query.LineRiskLevelQuery;
import com.koron.order.risk.levelChange.bean.vo.LineRiskLevelVO;
import com.koron.order.risk.levelChange.mapper.LineRiskLevelMapper;
import com.koron.order.risk.levelChange.service.LineRiskLevelService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 风险级别变更单(LineRiskLevel)表服务实现类
 *
 * @author jzp
 * @since 2022-12-16 15:52:25
 */
@Service
public class LineRiskLevelServiceImpl implements LineRiskLevelService {
    @Autowired
    private LineRiskService lineRiskService;
    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public LineRiskLevelVO getById(SessionFactory factory, String id) {
        LineRiskLevelMapper mapper = factory.getMapper(LineRiskLevelMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<LineRiskLevelVO> list(SessionFactory factory, LineRiskLevelQuery query) {
        LineRiskLevelMapper mapper = factory.getMapper(LineRiskLevelMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<LineRiskLevelVO> page(SessionFactory factory, PageQuery pageQuery, LineRiskLevelQuery query) {
        pageQuery.unPrefixOrders("lineRiskLevel", Arrays.asList("currNodeName",
                "currNodeUser",
                "currNodeCode"));
        return LineRiskLevelService.super.page(factory, pageQuery, query);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public LineRiskLevel save(SessionFactory factory, LineRiskLevel lineRiskLevel) {
        if (lineRiskLevel.getStatus() == null) {
            lineRiskLevel.setStatus("0");
        }
        if (lineRiskLevel.getCode() == null) {
            String prefix = lineRiskLevel.getOrderCode() + "_BG";
            TOrderNoMax tOrderNoMax = CodeTools.getNoMax(prefix, "y");
            String code = prefix + String.format("%03d", tOrderNoMax.getMaxValue());
            lineRiskLevel.setCode(code);
        }
        LineRiskLevelMapper mapper = factory.getMapper(LineRiskLevelMapper.class);
        mapper.insertOrUpdate(lineRiskLevel);
        return lineRiskLevel;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, LineRiskLevel lineRiskLevel) {
        LineRiskLevelMapper mapper = factory.getMapper(LineRiskLevelMapper.class);
        return mapper.update(lineRiskLevel) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        LineRiskLevel lineRiskLevel = new LineRiskLevel();
        lineRiskLevel.setId(id);
        lineRiskLevel.setStatus(status);
        return update(factory, lineRiskLevel);
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, LineRiskLevelVO lineRiskLevelVO) {
        // 保存基本信息
        lineRiskLevelVO.setStatus("2");
        save(factory, lineRiskLevelVO);

        // 修改风险管控单的数据 措施落实情况、风险详细信息
        LineRiskVO lineRiskVO = lineRiskService.getById(factory, lineRiskLevelVO.getOrderId());
        lineRiskVO.setRiskLevel(lineRiskLevelVO.getNewRiskLevel());
        lineRiskService.save(factory, lineRiskVO);
        return true;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        LineRiskLevelMapper mapper = factory.getMapper(LineRiskLevelMapper.class);
        return mapper.hidden(id) > 0;
    }
}
